package org.tpc.yn.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.tpc.yn.result.SaResult;
import org.tpc.yn.service.CallService;
import org.tpc.yn.utils.AESUtil;
import org.tpc.yn.utils.MD5Util;

import java.util.Map;

@RestController
@ResponseBody
@Slf4j
public class CallController {

    @Value("${API.APPSECRET}")
    private String appSecret;

    @Autowired
    private CallService callService;
    @PostMapping("allInterface")
    public SaResult call(@RequestParam Map<String,Object> params) {
        log.info("请求加密JSON:{}",params);
        JSONObject jsonObject = (JSONObject) JSON.toJSON(params);
        if (jsonObject == null) {
            return SaResult.error("参数为空");
        }
        if( jsonObject.getString("bscode") != null) {
            try {
                String bsCode = jsonObject.getString("bscode");
                String data = "";
                if(jsonObject.getString("data")!=null){
                    data = AESUtil.AESDecryptV2(jsonObject.getString("data"), appSecret);
                    log.info("请求解密JSON:{}",data);
                }
                JSONObject requestJSON = new JSONObject();
                requestJSON.put("P_BSCODE", bsCode);
                requestJSON.put("P_DATA", data);
                requestJSON.put("P_PROC_NAME","PROC_JK_DO_DOCBILL");
                requestJSON.put("P_ACTION",bsCode);
                JSONObject callJSON = new JSONObject();
                callJSON = callService.call(requestJSON);
                JSONObject responseJSON = new JSONObject();
                if(bsCode.equals("queryproduct")) {
                    responseJSON.put("total",callJSON.getJSONArray("DATASET").size());
                    responseJSON.put("list",callJSON.getJSONArray("DATASET"));
                    log.info("返回信息:{}",responseJSON);
                    String dataStr = AESUtil.AESEncryptV2(responseJSON.toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }else if(bsCode.equals("getstock")){
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset);
                    String dataStr = AESUtil.AESEncryptV2(dataset.toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }else if(bsCode.equals("saveorder")){
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset.getJSONObject(0));
                    String dataStr = AESUtil.AESEncryptV2(dataset.getJSONObject(0).toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }else if(bsCode.equals("cancelorder")){
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset.getJSONObject(0));
                    String dataStr = AESUtil.AESEncryptV2(dataset.getJSONObject(0).toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }else if(bsCode.equals("getorder")){
                    JSONObject dataJSON = callJSON.getJSONArray("DATASET").getJSONObject(0);
                    requestJSON.put("P_ACTION",bsCode+"-order");
                    requestJSON.put("P_BSCODE",bsCode+"-order");
                    JSONObject orderOrderJSON = callService.call(requestJSON);
                    JSONObject orderDetailJSON = orderOrderJSON.getJSONArray("DATASET").getJSONObject(0);
                    requestJSON.put("P_ACTION",bsCode+"-goods");
                    requestJSON.put("P_BSCODE",bsCode+"-goods");
                    JSONObject goodsJSON = callService.call(requestJSON);
                    orderDetailJSON.put("goods_detail",goodsJSON.getJSONArray("DATASET"));
                    JSONArray orderDetailArray = new JSONArray();
                    orderDetailArray.add(orderDetailJSON);
                    dataJSON.put("order_detail",orderDetailArray);
                    log.info("返回信息:{}",JSONObject.toJSONString(dataJSON));
                    String dataStr = AESUtil.AESEncryptV2(JSONObject.toJSONString(dataJSON), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }else if(bsCode.equals("takeorder")){
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset.getJSONObject(0));
                    String dataStr = AESUtil.AESEncryptV2(dataset.getJSONObject(0).toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                } else if (bsCode.equals("returnorder")) {
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset.getJSONObject(0));
                    String dataStr = AESUtil.AESEncryptV2(dataset.getJSONObject(0).toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                } else if (bsCode.equals("getreturnorder")) {
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset.getJSONObject(0));
                    String dataStr = AESUtil.AESEncryptV2(dataset.getJSONObject(0).toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }else if (bsCode.equals("getthxx")) {
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset.getJSONObject(0));
                    String dataStr = AESUtil.AESEncryptV2(dataset.getJSONObject(0).toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }else if (bsCode.equals("getthmx")) {
                    JSONArray dataset = callJSON.getJSONArray("DATASET");
                    log.info("返回信息:{}",dataset.getJSONObject(0));
                    String dataStr = AESUtil.AESEncryptV2(dataset.getJSONObject(0).toString(), appSecret);
                    String sign = MD5Util.encrypt(dataStr);
                    return SaResult.ok(dataStr,sign);
                }
            } catch (Exception e) {
                return SaResult.error("加密异常");
            }
        }else{
            return SaResult.error("业务代码为空");
        }
        return SaResult.error("业务代码为空");
    }
}
